package com.chushouya.controller.order.admin;

import com.chushouya.common.constant.ServiceType;
import com.general.framework.core.lang.Dates;
import com.general.framework.core.schema.Response;
import com.chushouya.order.dto.admin.stat.ManageDataDTO;
import com.chushouya.order.service.admin.ManageStatService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 运营统计数据控制器
 */
@Api(tags = ServiceType.PLATFORM + "运营统计数据")
@RestController
@RequestMapping(ServiceType.PLATFORM + "/manageStat")
@Slf4j
public class ManageStatController {

    @Resource
    private ManageStatService manageStatService;

    @ApiOperation("获取一个月的统计数据列表")
    @GetMapping("/monthlyList")
    public Response<List<ManageDataDTO>> getMonthlyStatList() {
        log.info("获取一个月的统计数据列表");
        List<ManageDataDTO> result = manageStatService.getMonthlyStatData();
        return Response.success(result);
    }

    @ApiOperation("获取一个月的汇总统计数据")
    @GetMapping("/monthlySummary")
    public Response<ManageDataDTO> getMonthlySummary() {
        log.info("获取一个月的汇总统计数据");
        ManageDataDTO result = manageStatService.getMonthlySummaryData();
        return Response.success(result);
    }

    @ApiOperation("获取指定日期范围的统计数据")
    @GetMapping("/dateRange")
    public Response<List<ManageDataDTO>> getStatByDateRange(
            @ApiParam(value = "开始日期", required = true, example = "2024-01-01")
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
            @ApiParam(value = "结束日期", required = true, example = "2024-01-31")
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate) {
        
        log.info("获取指定日期范围的统计数据，开始日期：{}，结束日期：{}", startDate, endDate);
        
        // 转换为当天的开始时间和结束时间
        Date start = Dates.getFirstTime(startDate);
        Date end = Dates.getLastTime(endDate);
        
        List<ManageDataDTO> result = manageStatService.getStatDataByDateRange(start, end);
        return Response.success(result);
    }

    @ApiOperation("获取指定日期范围的汇总统计数据")
    @GetMapping("/dateRangeSummary")
    public Response<ManageDataDTO> getStatSummaryByDateRange(
            @ApiParam(value = "开始日期", required = true, example = "2024-01-01")
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
            @ApiParam(value = "结束日期", required = true, example = "2024-01-31")
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate) {
        
        log.info("获取指定日期范围的汇总统计数据，开始日期：{}，结束日期：{}", startDate, endDate);
        
        // 转换为当天的开始时间和结束时间
        Date start = Dates.getFirstTime(startDate);
        Date end = Dates.getLastTime(endDate);
        
        // 直接使用Repository的汇总查询
        ManageDataDTO result = manageStatService.getMonthlySummaryData();
        return Response.success(result);
    }
}
